<template>
  <div class="wares">
    <!-- 左侧导航条 -->
    <ul class="left">
      <li class="kinds"
          v-for="(item,index) in kindsList"
          :key="index"
          :class=" {'clickLeft':true,'sel':selectIndex ==index}"
          @click="clickLeft(index)">
        <a :href="'#'+index"> {{item.name}} </a>

        <div class="kinds_left">
          <div :ref="'kinds_left_' + index"></div>
        </div>
      </li>
    </ul>
    <!-- 右边的第一次大循环 -->
    <div class="rList">
      <div class="right"
           v-for="(item1,index) in kindsList"
           :key="index">
        <p class="kindTit"
           :id="index">
          <span class="name1"> {{item1.name}} </span>
          <span class="name2"> {{item1.description}} </span>
          <span class="lue">...</span>
        </p>
        <!-- 商品 -->
        <div class="choose"
             v-for="(item2,index1) in item1.foods"
             :key="index1">
          <!-- 图片 -->
          <div class="cLeft">
            <div class="picBox">
              <span v-if="item2.attributes.length>0">
                <span v-for="(item3,index) in item2.attributes"
                      :key="index">
                  <span class="new"
                        v-if="item3 != null && item3.icon_name == '新' ">
                    <span class="newTxt"> 新</span>
                  </span>
                </span>
              </span>

              <img class="pic"
                   :src="url+item2.image_path"
                   alt="">
            </div>
            <div class="dtl">
              <!-- 商品名 -->
              <p class="fir">
                <span class="goodsN">{{item2.name}}</span>
                <span class="attr">
                  <span v-if="item2.attributes.length>0">
                    <span v-for="(item3,index) in item2.attributes"
                          :key="index">
                      <span v-if="item3 != null && item3.icon_name == '招牌' ">
                        <span class="sign">招牌</span>
                      </span>
                    </span>
                  </span>

                </span>

              </p>
              <p class="ep"> {{item2.description}} </p>
              <p class="sale"> 月售 {{item2.month_sales}}份 好评率{{item2.satisfy_rate}}% </p>
              <p class="amount"
                 v-if="item2.activity != null"> {{item2.activity.image_text}} </p>
              <div class="price">
                <div class="priLeft">
                  <span>￥</span>
                  <span class="pri"> {{item2.specfoods[0].price}} </span>
                </div>
                <!-- 规格 -->
                <div v-if="item2.specifications.length>0">
                  <span class="spec"
                        @click="showA = true">选规格</span>
                </div>
                <!-- 加减 -->
                <div class="cal" style="position: relative;"
                     v-else-if="item2.specifications.length ==0">
                  <span :class="{'sub':item2.__v}"
                        v-if="item2.__v > 0">
                    <i class="iconfont icon-jianhao"
                       @click="subAct(index,index1)"></i>
                    <span class="num"> {{item2.__v}} </span>
                  </span>
                  <i class="iconfont icon-jiahao"
                     @click="plusAct(index,index1)"></i>
                     <i :class="{'ball':true, 'run_top_right':run_qiu,'iconfont':true,'icon-jiahao':true}"
                     v-if="qiu_show==item2.item_id"></i>
                </div>
              </div>
            </div>
          </div>

        </div>
      </div>
    </div>
    <!-- 选规格弹出层 -->
    <van-overlay :show="showA">
      <div class="block">
        <div class="text">
          <span class="specTitle">1111</span>
          <i class="iconfont icon-chahao"
             @click="showA = false"></i>
        </div>
        <div class="selOne">
          <p class="guige">规格</p>
          <span v-for="(item4,indexA) in specData"
                :key="indexA"
                :class=" {'selSpec':true,'sel':select ==indexA}"
                @click="guigeAct(indexA)"> {{item4}} </span>
        </div>
        <div class="bott">
          <span class="money">
            <span class="doll">￥</span>
            <span class="price">20</span>
          </span>
          <p class="add">加入购物车</p>
        </div>
      </div>
    </van-overlay>

    <div class="foot">
      <div class="bottom">
        <div is-link
             @click="showPopup">
          <div class="shopCar" :class="{'zhendong': !qiu_show}"
               ref="gouwuche">
            <i class="iconfont icon-gouwuche"></i>
          </div>
          <div class="priNum">
            <p class="priData">￥{{Totalprice}}</p>
            <p class="fee"> 配送费￥5</p>
          </div>
        </div>
        <div class="total"
             ref="number"> {{sum}} </div>

        <div class="account"
             ref="bgc"
             @click="settlement">{{PriceDifference}}</div>
      </div>
    </div>

    <van-popup v-model="show"
               position="bottom"
               :style="{ height: '30%',bottom:'46px' }">
      <div class="car">
        <div class="carTop">
          <div class="carTit">购物车</div>
          <div class="delete">
            <i class="iconfont icon-lajitong"></i>
            <span @click="eliminate">清空</span>
          </div>
        </div>
        <ul class="orderList">
          <li class="orderL"
              v-for="(m,n) in orderData"
              :key="n">
            <div>
              <p class="goodsName">{{m.name}}</p>
              <p></p>
            </div>
            <div>
              <span class="mny">￥</span><span class="priNum">{{m.price}}</span>
            </div>

            <div class="carCount">
              <span class="carJian">
                <i class="iconfont icon-jianhao"
                   @click="carJian(n)"></i>
                <span class="carNum">{{m.num}}</span>
              </span>
              <i class="iconfont icon-jiahao"
                 @click="carPlus(n)"></i>
            </div>
          </li>

        </ul>
      </div>
    </van-popup>

  </div>
</template>
<script>
import Vue from 'vue';
import { Overlay } from 'vant';
import { Popup } from 'vant';

Vue.use(Popup);
Vue.use(Overlay);
export default {
  data: function () {
    return {
      kindsList: [],
      selectIndex: 0,
      select: 0,
      url: "//elm.cangdu.org/img/",
      show: false,
      showA: false,
      specData: ['默认', '111'],
      selNum: 0,
      // isShow:0,
      totalData: 0,
      orderData: [],

      run_qiu: false,
      qiu_show: 0,

    }
  },
  watch: {
    "Totalprice": {
      handler: function () {
        if (this.Totalprice >= 20) {
          this.$refs.bgc.style.backgroundColor = "#4cd964";
        } else {
          this.$refs.bgc.style.backgroundColor = "#535356";
        }
        //   console.log(newV)
      }
    },
    "sum": {
      handler: function () {
        if (this.sum > 0) {
          this.$refs.gouwuche.style.backgroundColor = "#3190e8";
          this.$refs.number.style.display = "block"
        } else {
          this.$refs.gouwuche.style.backgroundColor = "#3d3d3f";
          this.$refs.number.style.display = "none"
        }
        //   console.log(newV)
      }
    },
    deep: true,
    immediate: true
  },
  computed: {
    sum () {
      let suma = 0
      for (const key in this.orderData) {
        suma += this.orderData[key].num
      }
      //   console.log(suma);
      return suma
    },
    Totalprice () {
      // let suma = 0;
      let Totalprice = 0;
      for (const key in this.orderData) {

        Totalprice += this.orderData[key].num * this.orderData[key].price
      }
      return Totalprice
    },
    PriceDifference () {
      let Totalprice = 0;
      for (const key in this.orderData) {
        Totalprice += this.orderData[key].num * this.orderData[key].price
      }
      let wenzi = "";
      if (Totalprice == 0) {
        Totalprice = 20
        wenzi = "还差￥" + Totalprice + "起送";

      } else if (Totalprice < 20 && Totalprice != 0) {
        Totalprice = 20 - Totalprice
        wenzi = "还差￥" + Totalprice + "起送"
      } else if (Totalprice >= 20) {
        wenzi = "去结算"

        // this.$refs.bgc.style.backgroundColor="#4cd964"
        localStorage.setItem("totalprice", JSON.stringify(Totalprice))
      }
      return wenzi
    }
  }
  ,
  created () {
    this.$axios.get('https://elm.cangdu.org/shopping/v2/menu', {
      params: {
        restaurant_id: JSON.parse(localStorage.getItem('shopid'))
      }
    }).then(res => {
      // console.log(res);
      this.kindsList = res.data;
      //   console.log(this.kindsList)
    }).catch(err => console.log(err));
  },
  methods: {
    clickLeft (index) {
      this.selectIndex = index;
    },
    guigeAct (indexA) {
      this.select = indexA;
    },
    showPopup () {
      if (this.show == false) {
        this.show = true;
      } else {
        this.show = false;
      }

    },
    plusAct (index, index1) {
      // 小球
       this.qiu_show = this.kindsList[index].foods[index1].item_id
      this.run_qiu = true
      setTimeout(() => {
        this.qiu_show = 0
      }, 1000);

      // this.isShow ++
      this.kindsList[index].foods[index1].__v++;
      var arr = []
      for (var i = 0; i < this.orderData.length; i++) {
        arr[i] = this.orderData[i].id

      }

      //-1不相同，相同返回下标
      if (arr.indexOf(this.kindsList[index].foods[index1].item_id) != -1) {
        // console.log(444)
        this.orderData[arr.indexOf(this.kindsList[index].foods[index1].item_id)].num++
        // this.kindsList[index].foods[index1].__v ++;
      } else {
        let aa = {
          name: this.kindsList[index].foods[index1].name,
          num: 1,
          price: this.kindsList[index].foods[index1].specfoods[0].price,
          id: this.kindsList[index].foods[index1].item_id
        }

        this.orderData.push(aa)
      }

      //   this.$emit("jiaevent",this.orderData);
      //此处控制小圆球样式
      let kends = "kinds_left_" + index;
      let sum = 0;
      for (const key in this.kindsList[index].foods) {
        sum += this.kindsList[index].foods[key].__v;
      }
      //给标签添加样式
      for (const key in this.$refs) {
        if (kends == key) {
          this.$refs[key][0].style.width = "100%";
          this.$refs[key][0].style.height = "100%";
          this.$refs[key][0].style.backgroundColor = "rgba(255,0,0,1)";
          this.$refs[key][0].innerHTML = sum;
        }
      }

    },
    subAct (index, index1) {
      // this.isShow --;
      this.kindsList[index].foods[index1].__v--;

      if (this.kindsList[index].foods[index1].__v <= 0) {
        // this.isShow = 0;
        this.kindsList[index].foods[index1].__v = 0;
      }
      var arr = []
      for (var i = 0; i < this.orderData.length; i++) {
        arr[i] = this.orderData[i].id

      }
      this.orderData[arr.indexOf(this.kindsList[index].foods[index1].item_id)].num--

      let kends = "kinds_left_" + index;
      for (const key in this.$refs) {
        if (kends == key) {
          this.$refs[key][0].innerHTML--;
          if (this.$refs[key][0].innerHTML <= 0) {
            // this.$refs[key][0].style.backgroundColor = " #f5f5f5";
            this.$refs[kends][0].style.backgroundColor = "rgba(0,0,0,0)";
            this.$refs[key][0].innerHTML = "";
          }
        }
      }
    },
    carPlus (n) {
      for (const key in this.kindsList) {
        for (const keys in this.kindsList[key].foods) {
          if (this.kindsList[key].foods[keys].item_id == this.orderData[n].id) {
            this.kindsList[key].foods[keys].__v += 1;
            this.orderData[n].num++;
            let kends = "kinds_left_" + key;
            // console.log(this.$refs[kends]);
            this.$refs[kends][0].innerHTML = this.kindsList[key].foods[keys].__v;
          }
        }
      }
      // console.log(this.orderData);
    },
    carJian (n) {
      for (const key in this.kindsList) {
        for (const keys in this.kindsList[key].foods) {
          if (this.kindsList[key].foods[keys].item_id == this.orderData[n].id) {

            this.kindsList[key].foods[keys].__v--;
            this.orderData[n].num--;
            if (this.kindsList[key].foods[keys].__v > 0) {

              let kends = "kinds_left_" + key;
              let sum = 0;
              for (const keyA in this.kindsList[key].foods) {
                sum += this.kindsList[key].foods[keyA].__v;
              }
              this.$refs[kends][0].innerHTML = sum;
            } else if (this.kindsList[key].foods[keys].__v <= 0) {
              // console.log("qqqqqqqqq");
              let sum = 0;
              for (const keyA in this.kindsList[key].foods) {
                sum += this.kindsList[key].foods[keyA].__v;
            }

              let kends = "kinds_left_" + key;
              this.$refs[kends][0].innerHTML = sum;
              if(sum == 0){
                this.$refs[kends][0].style.backgroundColor = "rgba(0,0,0,0)";
              }
              this.orderData.splice(n, 1)
            }

          }
        }
      }
    },
    eliminate () {
      this.orderData = [];
      this.show = false;
      for (const key in this.kindsList) {
        for (const keys in this.kindsList[key].foods) {
          this.kindsList[key].foods[keys].__v = 0;
          let kends = "kinds_left_" + key;
          this.$refs[kends][0].innerHTML = "";
          // this.$refs[kends][0].style.backgroundColor = "#f5f5f5";
          this.$refs[kends][0].style.backgroundColor = "rgba(0,0,0,0)";
        }
      }
    },
    settlement () {
      let Totalprice = 0;
      for (const key in this.orderData) {
        Totalprice += this.orderData[key].num * this.orderData[key].price
      }
      if (Totalprice >= 20) {
        //将数据存在local里面
        // console.log(this.orderData);
        localStorage.setItem("settlement", JSON.stringify(this.orderData));
        // 去结算
        this.$router.push({ name: "Confirm" })
      }
    }
  },

}
</script>
<style lang="less">
.wares {
  .left::-webkit-scrollbar {
    display: none; /* Chrome Safari */ //隐藏滚动条
  }
  .left {
    width: 23%;
    height: 667px;
    background-color: #f5f5f5;
    position: fixed;
    top: 150px;
    left: 0;
    overflow-y: scroll;

    .kinds {
      width: 100%;
      text-align: center;
      padding: 18px 7px;
      border-bottom: 1px solid #eeeeee;
      box-sizing: border-box;
      position: relative;
      .kinds_left {
        width: 20px;
        height: 20px;
        font-size: 16px;
        color: white;
        text-align: center;
        line-height: 20px;
        // background-color: #f5f5f5;
        position: absolute;
        overflow: hidden;
        top: 0px;
        right: 0px;
        border-radius: 50%;
      }
      a {
        color: #666666;
        display: block;
        font-size: 15px;
      }
    }
    .clickLeft {
      background-color: #f5f5f5;
      border-left: 2px solid #f5f5f5;
    }
    .sel {
      background-color: #ffffff;
      border-left: 3px solid #3190e8;
    }
  }
  .rList {
    padding-bottom: 50px;
    .right {
      width: 77%;
      float: right;
      .kindTit {
        background-color: #f5f5f5;
        padding: 11px;
        .name1 {
          font-size: 16px;
          font-weight: 800;
          color: #666666;
        }
        .name2 {
          font-size: 13px;
          color: #999999;
        }
        .lue {
          float: right;
          color: #cccccc;
        }
      }
      .choose {
        padding: 10px;
        border-bottom: 1px solid #eeeeee;
        box-sizing: border-box;
        background-color: white;
        .picBox {
          display: inline-block;
          width: 18%;
          height: 46.5px;
          vertical-align: top;
          margin-right: 10px;
          position: relative;
          .pic {
            width: 100%;
            height: 100%;
          }
          .new {
            width: 0;
            height: 0;
            border-top: 28px solid #4cd964;
            border-right: 28px solid transparent;
            position: absolute;
            top: 0;
            left: 0;
            .newTxt {
              font-size: 12px;
              position: absolute;
              top: -30px;
              left: 0;
              transform: rotate(-45deg) translate(-0.5px, 1.5px);
              color: #ffffff;
            }
          }
        }

        .dtl {
          width: 76%;
          display: inline-block;
          .fir {
            width: 100%;
            height: 25px;
            .attr {
              position: relative;
            }
            .goodsN {
              width: 100px;
              color: #333333;
              font-size: 16px;
              font-weight: 900;
              white-space: nowrap;
              text-overflow: ellipsis;
              overflow: hidden;
              float: left;
            }
            .sign {
              font-size: 12px;
              color: red;
              border: 1px solid red;
              border-radius: 40%;
              float: right;
            }
          }

          .ep {
            font-size: 12px;
            color: #cccccc;
            width: 100px;
            white-space: nowrap;
            text-overflow: ellipsis;
            overflow: hidden;
          }
          .sale {
            font-size: 12px;
            color: #333333;
          }
          .amount {
            font-size: 12px;
            color: rgb(241, 136, 79);
            border: 1px solid rgb(240, 115, 115);
            border-radius: 11px;
            display: inline-block;
          }
          .price {
            font-size: 15px;
            .priLeft {
              color: #f60;
              float: left;
              .pri {
                font-weight: 900;
              }
            }
            .cal {
              float: right;
              .iconfont {
                color: #3190e8;
                font-size: 20px;
              }
              .num {
                margin: 0 5px;
              }
              // .sub{
              // display: none;
              // }
            }
            .spec {
              font-size: 12px;
              float: right;
              background-color: #3190e8;
              color: #ffffff;
              padding: 2.3px 4.68px;
              border-radius: 5px;
            }
          }
        }
      }
    }
  }
  .van-overlay {
    z-index: 30;
    .block {
      width: 262px;
      height: 200px;
      margin: 50% 15%;
      background-color: #ffffff;
      box-sizing: border-box;
      .text {
        text-align: center;
        padding: 11.7px 11.7px 9px 11.7px;
        i {
          font-size: 25px;
          float: right;
        }
        .specTitle {
          font-size: 16px;
          color: #222222;
        }
      }
      .guige {
        font-size: 14px;
        color: #666666;
        margin-bottom: 5px;
      }
      .selOne {
        padding: 9px 11.7px;
        margin-bottom: 35px;
        .selSpec {
          color: #333333;
          font-size: 14px;
          margin: 0 11.7px 5px 0;
          padding: 7px 11px;
          border: 1px solid #dddddd;
          border-radius: 5px;
        }
        .sel {
          color: #3190e8;
          border: 1px solid #3190e8;
        }
      }
      .bott {
        padding: 12px;
        background-color: #f9f9f9;
        height: 54px;
        box-sizing: border-box;
        .money {
          color: #ff6000;
          float: left;
          .doll {
            font-size: 12px;
          }
          .price {
            font-size: 18.75px;
          }
        }
        .add {
          width: 93.75px;
          height: 30.4px;
          text-align: center;
          line-height: 30.4px;
          border-radius: 5px;
          float: right;
          font-size: 14px;
          background-color: #3199e8;
          color: #ffffff;
        }
      }
    }
  }
  .foot {
    height: 46.88px;
    overflow: hidden;
    .bottom {
      width: 100%;
      height: 46.88px;
      box-sizing: border-box;
      background-color: #3d3d3f;
      position: fixed;
      bottom: 0;
      left: 0;
      z-index: 100;
      .shopCar {
        display: inline-block;
        background-color: #3d3d3f;
        border: 4px solid #444444;
        padding: 6px;
        box-sizing: border-box;
        border-radius: 50%;
        position: absolute;
        left: 10px;
        top: -20px;
        .iconfont {
          color: #f5f5f5;
          font-size: 35px;
        }
      }
      .priNum {
        display: inline-block;
        position: absolute;
        top: 3px;
        left: 20%;
        color: #ffffff;
        .priData {
          font-size: 18.75px;
        }
        .fee {
          font-size: 12px;
        }
      }
      .total {
        position: absolute;
        top: -20px;
        left: 50px;
        width: 16.4px;
        height: 16.4px;
        font-size: 12px;
        color: white;
        background-color: red;
        border-radius: 50%;
        text-align: center;
        line-height: 16.4px;
        display: none;
      }
      .account {
        width: 117px;
        height: 100%;
        background-color: #535356;
        position: absolute;
        top: 0;
        right: 0;
        text-align: center;
        color: #ffffff;
        font-size: 16px;
        line-height: 46px;
        font-family: "Yahei";
        font-weight: 600;
      }
      .bgc {
        background-color: #4cd964;
      }
    }
  }
  .car {
    color: #666666;
    .carTop {
      background-color: #eceff1;
      padding: 7.5px 14px;
      overflow: hidden;
      .carTit {
        font-size: 16.4px;
        float: left;
      }
      .delete {
        font-size: 14px;
        float: right;
        i {
          margin-right: 5px;
          font-size: 17px;
        }
      }
    }
    .orderList {
      .orderL {
        padding: 10px;
        display: flex;
        justify-content: space-between;
        .goodsName {
          font-size: 16.5px;
          width: 193px;
          font-weight: bold;
        }
        .mny {
          font-size: 14px;
          color: #ff6600;
        }
        .priNum {
          font-size: 17px;
          color: #ff6600;
          font-weight: bold;
        }
        .carCount {
          .carNum {
            font-size: 16px;
            margin: 0 5px;
          }
          i {
            color: #3190e8;
            font-size: 20px;
          }
        }
      }
    }
  }
  
}
.zhendong {
  animation: bounceIn;
  animation-duration: 0.7s;
}
.ball {
  position: absolute;
  z-index: 10;
  right: 0;
  top: 0px;
  font-size: 20px;
  color: #3190e8;
}

//定义动画的流程
.run_top_right {
  display: block;
  animation: run-right-right 0.5s 0.4s 1 linear,
    run-right-top 0.5s 0.4s 1 cubic-bezier(0.66, 0.1, 1, 0.41);
  // animation: run-right-right 3s 0.4s 1 linear, run-right-top 3s 0.4s 1 ease-out;
  animation-fill-mode: forwards;
}

//向上走的动画初始及结尾值
@keyframes run-right-top {
  0% {
    top: 0px;
  }
  100% {
    top: 400px;
  }
}
//向上右的动画初始及结尾值
@keyframes run-right-right {
  0% {
    right: 0;
    // transform: scale(0.7);
  }

  100% {
    right: 300px;
    // transform: scale(0.45);
  }
}
</style>